package pannelli;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;

import database.DB;

import javax.swing.JButton;

import javax.swing.JOptionPane;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


import oggetti.Utente;
import pannelli.main.PannelloUtenti;
import pannelli.sub.SubPannelloUtente;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class AggiungiUtente extends javax.swing.JFrame {
	private SubPannelloUtente subPannelloUtente1;
	private JButton aggiungi;
		
	public AggiungiUtente() {
		super();
		initGUI();
		centra();
		setVisible(true);
	}
	public void centra(){
		Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(new Point((dimension.width - getSize().width) / 2, 
				(dimension.height - getSize().height) / 2 ));
	}
	private void initGUI() {
		try {
			AnchorLayout thisLayout = new AnchorLayout();
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				aggiungi = new JButton();
				getContentPane().add(aggiungi, new AnchorConstraint(724, 638, 857, 333, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				aggiungi.setText("Aggiungi utente");
				aggiungi.setPreferredSize(new java.awt.Dimension(122, 37));
				aggiungi.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent arg0) {
						Utente u = subPannelloUtente1.getUtenteFromFields();
						
						
						if (u.getUsername().equals("") && u.getPassword().equals(""))
							JOptionPane.showMessageDialog(null, "I campi username e Password sono obbligatori!");
						else{
							if(new DB().addUtente(u)){
								JOptionPane.showMessageDialog(null,"Utente aggiunto con successo!");
								dispose();
								PannelloUtenti.getInstance().initTable();
								PannelloUtenti.getInstance().setFlag(false);
							}else{
								JOptionPane.showMessageDialog(null,"Si è verificato un errore mentre aggiungevo l'utente!");
							}
						}
					}
								
					}
				);
			}
			{
				subPannelloUtente1 = new SubPannelloUtente();
				getContentPane().add(subPannelloUtente1, new AnchorConstraint(1, 1001, 649, 1, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
				subPannelloUtente1.setPreferredSize(new java.awt.Dimension(400, 180));
			}
			pack();
			setSize(400, 300);
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}

}
